package com.zju.DoctorGo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zju.DoctorGo.entity.Medicine;
import com.zju.DoctorGo.entity.Prescription;
import com.zju.DoctorGo.entity.Review;
import com.zju.DoctorGo.mapper.MedicineMapper;
import com.zju.DoctorGo.mapper.PrescriptionMapper;
import com.zju.DoctorGo.service.MedicineService;
import com.zju.DoctorGo.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class PrecriptionServiceImpl implements PrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public void createPrescription(Prescription prescription) {
        prescriptionMapper.addPrescription(prescription);
    }

    @Override
    public void deletePrescription(Long id) {
        prescriptionMapper.deletePrescription(id);
    }

    @Override
    public List<Prescription> getPrescription(Long id) {
        //List<Prescription> prescription =  prescriptionMapper.getPrescriptionById(id);
        List<Prescription> prescription = prescriptionMapper.selectList(new QueryWrapper<Prescription>().eq("prescriptionId", id));
        return prescription;
    }

    @Override
    public List<Prescription> getAllPrescriptions() {
        List<Prescription> prescriptions = prescriptionMapper.getPrescriptions();
        return prescriptions;
    }

    @Override
    public double getTotalPrice(Long prescriptionId) {
        List<Prescription> prescriptions =  prescriptionMapper.getPrescriptionsByPrescriptionId(prescriptionId);
        //System.out.println("Prescription count for ID " + prescriptionId + ": " + prescriptions.size());
        return calculateTotalPrice(prescriptions);
    }

    public double calculateTotalPrice(List<Prescription> prescriptions) {
        System.out.println("Calculating total price for " + prescriptions.size() + " prescriptions.");
        double totalPrice = 0.0;
        try {
            for (Prescription p: prescriptions) {
                try {
                    Medicine medicine = medicineMapper.getMedicineById(p.getMedicineId());
                    System.out.println("Medicine ID " + p.getMedicineId() + ", Unit Price: " + medicine.getUnitPrice());
                    BigDecimal quantity = new BigDecimal(p.getQuantity());
                    BigDecimal unitPrice = medicine.getUnitPrice();
                    double totalPriceForMedicine = quantity.multiply(unitPrice).doubleValue();
                    totalPrice += totalPriceForMedicine;
                } catch (Exception e) {
                    System.err.println("Error calculating price for medicine ID " + p.getMedicineId());
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            System.err.println("Error during total price calculation");
            e.printStackTrace();
        }
        return totalPrice;
    }

    @Override
    public void updatePrescriptionItem(Long prescriptionId, Long medicineId, int quantity) {
        Integer updatedRows = prescriptionMapper.updatePrescriptionItem(prescriptionId, medicineId, quantity);
        if (updatedRows == null || updatedRows != 1) {
            throw new RuntimeException("Failed to update prescription item.");
        }
    }

    @Override
    public void deletePrescriptionItem(Long prescriptionId, Long medicineId) {
        Integer deletedRows = prescriptionMapper.deletePrescriptionItem(prescriptionId, medicineId);
        if (deletedRows == null || deletedRows != 1) {
            throw new RuntimeException("Failed to delete prescription item.");
        }
    }

    @Override
    public void addPrescriptionItem(Long prescriptionId, Long medicineId, int quantity) {
        Integer insertedRows = prescriptionMapper.insertPrescriptionItem(prescriptionId, medicineId, quantity);
        if (insertedRows == null || insertedRows != 1) {
            throw new RuntimeException("Failed to add prescription item.");
        }
    }
}